package cn.share.life.module.images.utils;

import cn.hutool.core.io.FileUtil;
import com.google.common.collect.Lists;

import java.io.File;
import java.util.List;
import java.util.Map;

public class CheckDeleteAndUpdateFileNameUtils {

    public static void main(String[] args) {
        HtmlFidAndPathEnums[] values = HtmlFidAndPathEnums.values();
        for (HtmlFidAndPathEnums value : values) {
            renameFileCount(value.getBasePath());
        }
    }

    /**
     * 重置文件名
     */
    private static void renameFileCount(String path) {
        System.out.println("开始: " + path);
        List<File> urlFileList = ImageFileUtils.getUrlFileList(path);
        if (urlFileList.isEmpty()) {
            System.out.println("没有链接文件: " + path);
            return;
        }

        Map<String, String> imageFileList = ImageFileUtils.getImageFileNameMap(path);
        if (imageFileList.isEmpty()) {
            System.out.println("没有图片文件: " + path);
            return;
        }
        System.out.println("文件数量: " + imageFileList.size());

        for (File urlFile : urlFileList) {
            List<String> strings = FileUtil.readLines(urlFile, "UTF-8");
            List<String> urlList = Lists.newArrayList(strings);
            for (String string : urlList) {
                String fileName = string.substring(string.lastIndexOf("/") + 1);
                if (imageFileList.containsKey(fileName)) {
                    strings.remove(string);
                }
            }

            if (strings.isEmpty()) {
                String name = FileUtil.mainName(urlFile);
                System.out.println("文件已经下载完成: " + urlFile.getPath());
                FileUtil.rename(urlFile, "success_" + name, true, true);
            }
        }
    }


}
